/**
 * Created by jerome.arnoult on 23/12/14.
 */
function gamedebug(){
    this.mapGrid = false;
    this.mapGridCoords = false;
    this.mapGridAStarCoords = false;
    this.mapGridExplorer = false;
    this.mapGridExplorerCreated = false;
    this.fpsToggle = false;


}

//DEBUG
gamedebug.prototype.gridToggle = function(){
    if(game.gameDebug.mapGrid){
        game.gameDebug.debugGridGroup.visible = false;
    }else{
        if(game.gameDebug.debugGridGroup == undefined){
            game.gameDebug.debugGridGroup = new createjs.Container();
            var pair = true;
            for(var row in game.gameMap.grid.cases){
                for(var col in game.gameMap.grid.cases[row]){
                    var posx = col * game.gameMap.tileWidth;
                    var posy = row * (game.gameMap.tileHeight/2);
                    if(!pair)
                        posx += (game.gameMap.tileWidth/2);

                    var shape = new createjs.Shape();
                    shape.regX = 0;
                    shape.regY = 0;
                    shape.x = posx;
                    shape.y = posy;
                    shape.graphics
                        .beginStroke("#E0E0E0")
                        .moveTo(0, 0 - (game.gameMap.tileHeight/2))
                        .lineTo(0 - (game.gameMap.tileWidth/2), 0 - (game.gameMap.tileHeight))
                        .lineTo(0, 0 - (game.gameMap.tileHeight/2*3))
                        .lineTo((game.gameMap.tileWidth/2), 0 - (game.gameMap.tileHeight))
                        .lineTo(0, 0 - (game.gameMap.tileHeight/2))
                        .endStroke();

                    game.gameDebug.debugGridGroup.addChild(shape);
                }
                pair = !pair;
            }

            game.stage.addChild(game.gameDebug.debugGridGroup);
            game.gameDebug.debugGridGroup.x = game.mainContainer.x;
            game.gameDebug.debugGridGroup.y = game.mainContainer.y;
        }
        game.gameDebug.debugGridGroup.visible = true;
    }
    game.gameDebug.mapGrid = !game.gameDebug.mapGrid;
}

gamedebug.prototype.gridToggleCoords = function(){
    if(game.gameDebug.mapGridCoords){
        game.gameDebug.debugGridInfosGroup.visible = false;
    }else{
        if(game.gameDebug.debugGridInfosGroup == undefined){
            game.gameDebug.debugGridInfosGroup = new createjs.Container();
            var pair = true;
            for(var row in game.gameMap.grid.cases){
                for(var col in game.gameMap.grid.cases[row]){
                    var posx = col * game.gameMap.tileWidth;
                    var posy = row * (game.gameMap.tileHeight/2);
                    if(!pair)
                        posx += (game.gameMap.tileWidth/2);

                    var text = new createjs.Text(col+"X"+row, "6px Arial","#FF0D0D");
                    text.regX = 0;
                    text.regY = 0;
                    text.x = posx+8;
                    text.y = posy+2;

                    game.gameDebug.debugGridInfosGroup.addChild(text);
                }
                pair = !pair;
            }

            game.stage.addChild(game.gameDebug.debugGridInfosGroup);
            game.gameDebug.debugGridInfosGroup.x = game.mainContainer.x;
            game.gameDebug.debugGridInfosGroup.y = game.mainContainer.y;
        }
        game.gameDebug.debugGridInfosGroup.visible = true;
    }
    game.gameDebug.mapGridCoords = !game.gameDebug.mapGridCoords;
}

gamedebug.prototype.gridToggleAStarCoords = function(){
    if(game.gameDebug.mapGridAStarCoords){
        game.gameDebug.debugGridAStarGroup.visible = false;
    }else{
        if(game.gameDebug.debugGridAStarGroup == undefined){
            game.gameDebug.debugGridAStarGroup = new createjs.Container();
            var pair = true;
            for(var row in game.gameMap.grid.cases){
                for(var col in game.gameMap.grid.cases[row]){
                    var posx = col * game.gameMap.tileWidth;
                    var posy = row * (game.gameMap.tileHeight/2);
                    if(!pair)
                        posx += (game.gameMap.tileWidth/2);


                    var AStarCoords = game.gameMap.convertGridCoordsToAStarCoords([col,row]);

                    var text = new createjs.Text(AStarCoords[0]+"X"+AStarCoords[1], "6px Arial","#00EEFF");
                    text.regX = 0;
                    text.regY = 0;
                    text.x = posx+8;
                    text.y = posy+8;

                    game.gameDebug.debugGridAStarGroup.addChild(text);
                }
                pair = !pair;
            }

            game.stage.addChild(game.gameDebug.debugGridAStarGroup);
            game.gameDebug.debugGridAStarGroup.x = game.mainContainer.x;
            game.gameDebug.debugGridAStarGroup.y = game.mainContainer.y;
        }
        game.gameDebug.debugGridAStarGroup.visible = true;
    }
    game.gameDebug.mapGridAStarCoords = !game.gameDebug.mapGridAStarCoords;
}

gamedebug.prototype.gridToggleExplorer = function(){
    if(game.gameDebug.mapGridExplorer){
        game.gameDebug.debugGridAStarGroup.visible = false;
        $("#debugBar .gridExplorer").hide();
    }else{
        if(!game.gameDebug.mapGridExplorerCreated){
            game.gameDebug.debugGridExplorerGroup = new createjs.Container();
            $('#gamemap area').on('mouseover',function(e){
                var ID = e.target.id;
                var coords = ID.split('-');
                var asproperty = e.target.getAttribute('data-astar');
                $("#debugBar .gridExplorer .pcoords").html(coords[0]+"X"+coords[1]);
                var AStarCoords = game.gameMap.convertGridCoordsToAStarCoords(coords);
                $("#debugBar .gridExplorer .ascoords").html(AStarCoords[0]+"X"+AStarCoords[1]);
                $("#debugBar .gridExplorer .asproperty").html(asproperty.replace('#cell',''));
                $("#debugBar .gridExplorer .asconvert").html(game.gameMap.convertAStarCoordsToGridCoords([AStarCoords[0],AStarCoords[1]]).join("X"));
                if((AStarCoords[0]>>0+AStarCoords[1]>>0)%2 == 0){
                    $("#debugBar .gridExplorer .asweight").html("0");
                }else{
                    $("#debugBar .gridExplorer .asweight").html("1");
                }
            });
            game.gameDebug.mapGridExplorerCreated = true;
        }
        game.gameDebug.debugGridExplorerGroup.visible = true;
        $("#debugBar .gridExplorer").show();
    }
    game.gameDebug.mapGridExplorer = !game.gameDebug.mapGridExplorer;
}

gamedebug.prototype.drawingToggleFPS = function(){
    game.gameDebug.fpsToggle = !game.gameDebug.fpsToggle;
    game.gameRenderer.toggleFPS = game.gameDebug.fpsToggle;
    if(game.gameDebug.fpsToggle)
        game.gameDebug.drawingFPS();
    else
        $("#debugBar .fps").html("");
}

gamedebug.prototype.drawingFPS = function(){
    if(game.gameDebug.fpsToggle){
        $("#debugBar .fps").html(game.gameRenderer.FPS >> 0);
        setTimeout(game.gameDebug.drawingFPS, 1000);
    }
}



